import asyncio

from client import AgentClient
from core import settings
from schema import ChatMessage,CommandInfo
from uuid import uuid4


# async def amain() -> None:
#     #### ASYNC ####
#     client = AgentClient(settings.BASE_URL)

#     print("Agent info:")
#     print(client.info)

#     print("Chat example:")
#     response = await client.ainvoke("Tell me a brief joke?", model="deepseek-v3")
#     response.pretty_print()

#     print("\nStream example:")
#     async for message in client.astream("Share a quick fun fact?"):
#         if isinstance(message, str):
#             print(message, flush=True, end="")
#         elif isinstance(message, ChatMessage):
#             print("\n", flush=True)
#             message.pretty_print()
#         else:
#             print(f"ERROR: Unknown type - {type(message)}")


# def main() -> None:
#     #### SYNC ####
#     client = AgentClient(base_url=settings.BASE_URL,agent="chatbot")

#     print("Agent info:")
#     print(client.info)

#     print("Chat example:")
#     response = client.invoke("Tell me a brief joke?", model="deepseek-v3")
#     response.pretty_print()

#     print("\nStream example:")
#     for message in client.stream("Share a quick fun fact?", model="deepseek-v3"):
#         if isinstance(message, str):
#             print(message, flush=True, end="")
#         elif isinstance(message, ChatMessage):
#             print("\n", flush=True)
#             message.pretty_print()
#         else:
#             print(f"ERROR: Unknown type - {type(message)}")



# def main() -> None:
#     #### SYNC ####
#     client = AgentClient(base_url=settings.BASE_URL,agent="test_interupt")

#     print("\nStream example:")
#     for message in client.stream("what's the weather in beijing?", thread_id="123"):
#         if isinstance(message, str):
#             print("\n str \n", flush=True)
#             print(message, flush=True, end="")
#         elif isinstance(message, ChatMessage):
#             print("\n ChatMessage\n", flush=True)
#             message.pretty_print()
#         else:
#             print(f"ERROR: Unknown type - {type(message)}")


def main() -> None:
    #### SYNC ####
    client = AgentClient(base_url=settings.BASE_URL,agent="test_interupt")
    thread_id = str(uuid4())
    print("\n开始执行++++++++++++++++\n")
    for message in client.stream("what's the weather in beijing?", thread_id=thread_id):
        if isinstance(message, str):
            print("\n str \n", flush=True)
            print(message, flush=True, end="")
        elif isinstance(message, ChatMessage):
            print("\n ChatMessage\n", flush=True)
            message.pretty_print()
            print(message.tool_calls)
        else:
            print(f"ERROR: Unknown type - {type(message)} - {message}")
    print("\n断点执行，人工介入++++++++++++++++\n")
    next_node = client.next_state(thread_id=thread_id)
    print(next_node)


    print("\n继续执行++++++++++++++++\n")
    for message in client.stream(
        # provide value
        command=CommandInfo(action="update",data={"city":"北京,中国"}),
        thread_id=thread_id,
    ):
        if isinstance(message, str):
            print("\n str \n", flush=True)
            print(message, flush=True, end="")
        elif isinstance(message, ChatMessage):
            print("\n ChatMessage\n", flush=True)
            message.pretty_print()
            print(message.tool_calls)
        else:
            print(f"ERROR: Unknown type - {type(message)}")
    

if __name__ == "__main__":
    print("Running in sync mode")
    main()
    # print("\n\n\n\n\n")
    # print("Running in async mode")
    # asyncio.run(amain())
